﻿#include "SetColValDlg.h"
#include "ui_SetColValDlg.h"
#include <QValidator>
#include <QDoubleValidator>

SetColValDlg::SetColValDlg(int StartRowNo, int EndRowNo, QString StrStart, double DataStart, QString StrFocus, double DataFocus, bool bNumValue, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SetColValDlg)
{
	setModal(true);
    ui->setupUi(this);
	setFixedSize(348, 300);
	ui->pStartRowEdit->setReadOnly(true);
	ui->pEndRowEdit->setReadOnly(true);
	ui->pContentEdit->setReadOnly(true);
	m_StartRowNo = 0;
	m_EndRowNo = 0;
	m_ValueStart = 0;
	m_ValueStep = 0;
	m_StrFill.clear();
	m_DataFill=0;
	m_StrStart.clear();
	m_DataStart = 0;
	m_StrFocus.clear();
	m_DataFocus = 0;
	m_iType = 0;
	m_bAddFlag = true;
	m_bSelectStart = false;
	m_bNumerFlag = false;
	m_StartRowNo = StartRowNo;
	m_EndRowNo = EndRowNo;
	m_StrStart = StrStart;
	m_StrFocus = StrFocus;
	m_DataStart = DataStart;
	m_DataFocus = DataFocus;
	ui->pStartRowEdit->setText(QString::fromLocal8Bit("%1").arg(m_StartRowNo));
	ui->pEndRowEdit->setText(QString::fromLocal8Bit("%1").arg(m_EndRowNo));
	m_bNumerFlag = bNumValue;
	if (m_bNumerFlag)
	{
		m_ValueStart = m_DataStart;
		m_ValueStep = 1;
		ui->pStartValEdit->setText(QString::fromLocal8Bit("%1").arg(m_ValueStart));
		ui->pStepValEdit->setText(QString::fromLocal8Bit("%1").arg(m_ValueStep));		
		
		ui->pFocusRadioBtn->setEnabled(false);
		ui->pStartRadioBtn->setEnabled(false);
		ui->pAddRadioBtn->setChecked(true);
		m_bAddFlag = true;
		connect(ui->pAddRadioBtn, SIGNAL(clicked(bool)), this, SLOT(CheckAddFlag()));
		connect(ui->pMulRadioBtn, SIGNAL(clicked(bool)), this, SLOT(CheckAddFlag()));
		QValidator *validator = new QDoubleValidator(parent);		
		ui->pStartValEdit->setValidator(validator);
		ui->pStepValEdit->setValidator(validator);
	}else
	{
		ui->pContentEdit->setText(m_StrStart);
		ui->pAddRadioBtn->setEnabled(false);
		ui->pMulRadioBtn->setEnabled(false);
		ui->pStartRadioBtn->setChecked(true);
		m_bSelectStart = true;
		connect(ui->pFocusRadioBtn, SIGNAL(clicked(bool)), this, SLOT(CheckSelectFlag()));
		connect(ui->pStartRadioBtn, SIGNAL(clicked(bool)), this, SLOT(CheckSelectFlag()));
	}
	connect(ui->pOkBtn, SIGNAL(clicked(bool)), this, SLOT(Affirm()));
	connect(ui->pCauseBtn, SIGNAL(clicked(bool)), this, SLOT(Cancel()));
}

SetColValDlg::~SetColValDlg()
{
    delete ui;
}

void SetColValDlg::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//
void SetColValDlg::CheckAddFlag()
{
	if (ui->pAddRadioBtn->isChecked())
	{
		m_bAddFlag = true;
	}else if (ui->pMulRadioBtn->isChecked())
	{
		m_bAddFlag = false;
	}
}

void SetColValDlg::CheckSelectFlag()
{
	if (ui->pStartRadioBtn->isChecked())
	{
		m_bSelectStart = true;
		ui->pContentEdit->setText(m_StrStart);
	}else if (ui->pFocusRadioBtn->isChecked())
	{
		m_bSelectStart = false;
		ui->pContentEdit->setText(m_StrFocus);
	}
}

void SetColValDlg::Affirm()
{
	if (m_bNumerFlag)
	{
		m_ValueStart = ui->pStartValEdit->text().toDouble();
		m_ValueStep = ui->pStepValEdit->text().toDouble();
	}
	accept();
}

void SetColValDlg::Cancel()
{
	reject();
}

void SetColValDlg::GetParam(bool &bAddFlag, double &ValueStart, double &ValueStep, bool &bSelectStart)
{
	bAddFlag = m_bAddFlag;
	ValueStart = m_ValueStart;
	ValueStep = m_ValueStep;
	bSelectStart = m_bSelectStart;
}